// Store old onload information
var _OldOnload = null;
_OldOnload = window.onload;

function DoScrollBarHiJacks()
{
	// Call the original onload stuff first
	if( _OldOnload != null ) _OldOnload();
	// Hijack the postback routine
	HiJackPostBack();
	// Force automated scroll restoration
	__RESTORE_SCROLL();
}

// variable to store original __doPostBack
var _HiJackOldPostBack = null;
// function to replace original __doPostBack with our own version
function HiJackPostBack()
{
	// Now hijack the postback command
	_HiJackOldPostBack = __doPostBack;
	__doPostBack = HiJackedPostBack;
}

// our postback function - stores scroll position to hidden field first
function HiJackedPostBack( ctl, args )
{
	var field = document.getElementById("__SCROLLBARSTATE");
	// non ie
	if( field == null )
	{
		var formObj = document.forms[0];
		if( formObj )
			field = eval( "formObj.__SCROLLBARSTATE");
	}
	
	if( document.body )
		field.value = document.body.scrollLeft + "," + document.body.scrollTop;
	else
		field.value = window.pageXOffset + "," + window.pageYOffset;

//alert("Go with " + field.value);
	// Call original __doPostBack
	_HiJackOldPostBack( ctl, args );
}

// __RESTORE_SCROLL - Automatically restores the last known scrollbar position
// the hidden field __SCROLLBARSTATE is populated from either the scrollTo url 
// parameter or by the last known value by the hijacked postback.
function __RESTORE_SCROLL()
{
	var field = document.getElementById("__SCROLLBARSTATE");
	// non ie
	if( field == null )
	{
		var formObj = document.forms[0];
		if( formObj )
			field = eval( "formObj.__SCROLLBARSTATE");
	}
	if( field == null ) return;
	if( field.value == null ) return;
	if( field.value == "" ) return;
	
	var data = field.value.split(",");
	if( data.length < 2 ) return;

//alert("loading " + data[0] + "," + data[1]);	
	if( isNaN(data[0]) ) return;
	if( isNaN(data[1]) ) return;
	window.scrollTo( data[0], data[1] );
}
// tell document to execute hijack function when it's loaded
window.onload = DoScrollBarHiJacks;
